package org.zjx.analyse.action;

import com.sun.jna.platform.win32.WinDef;
import org.zjx.analyse.AbstractAction;
import org.zjx.analyse.IWindowAction;
import org.zjx.analyse.ScreenshotAnalyse;
import org.zjx.resolution.ResolutionButton;
import org.zjx.resolution.resolution.StartWindowResolution;
import org.zjx.util.HwndUtil;
import org.zjx.util.ImageUtil;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.List;

public class StartWindowAction extends AbstractAction implements IWindowAction {
    private final BufferedImage startButtonImage;

    public StartWindowAction(ScreenshotAnalyse screenshotAnalyse) {
        super(new StartWindowResolution(), screenshotAnalyse);
        ResolutionButton startButton = resolution.getButton(StartWindowResolution.START_WINDOW_START_BUTTON_RECT);
        startButtonImage = ImageUtil.stringToBufferedImage(startButton.getPattern());
    }

    @Override
    public boolean isOpened() {
        WinDef.HWND hwnd = gameWindow.getHwnd();
        return hwnd != null && HwndUtil.haveHwnd(hwnd) && !HwndUtil.getWindowTitle(hwnd).contains("[");
    }

    @Override
    public void open() {

    }

    @Override
    public void close() {
        HwndUtil.closeWindow(gameWindow.getHwnd());
    }

    @Override
    public void reSetWindow() {
        gameWindow.move(gameWindow.getInitPoint());
    }

    public void clickStartButton() {
        gameWindow.flush();
        BufferedImage screenshot = gameWindow.getScreenshot();
        List<Point> points = ImageUtil.matchTemplate(screenshot, startButtonImage);
        if (!points.isEmpty()) {
            Point point = new Point(points.get(0).x +  startButtonImage.getWidth()/2, points.get(0).y +  startButtonImage.getHeight()/2);
            gameWindow.clickMouse(point);
        }
    }
}
